var ctxPath = "/education";

$.getUrlParam = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
};

!function ($) {
    var $tab = $('#J_Tab');

    $tab.find('.tab-nav-item').on('opened.ydui.tab', function (e) {
        console.log('索引：%s - [%s]已经打开了', e.index, $(this).text());
    });

    var activeTab = $.getUrlParam('tab');
    if(null != activeTab && "" != activeTab){
        $("#" + activeTab).addClass("tab-active");
        $("." + activeTab).addClass("tab-active");
    }else{
        $("#all-tab").addClass("tab-active");
        $(".all-tab").addClass("tab-active");
    }

    var initOrders = function() {
        $.ajax({
            url: ctxPath + '/wx/api/pointReward/queryOrders',
            type: 'post',
            dataType: 'json',
            success: function (result) {
                if(result.returnCode == 0) {

                    var alls = result.alls;
                    var deliveries = result.deliveries;
                    var pays = result.pays;
                    var completes = result.completes;
                    if(alls.length > 0){
                        $(".all-tab").html("");

                        $(alls).each(function(index, ele){
                            $(".all-tab").append("<div class=\"pro-box pay-num\"><span>订单编号:" + ele.orderCode + "</span><span class=\"floatr text-red\">" + ele.orderStatus + "</span></div>");
                            $(".all-tab").append("<article class=\"m-list list-theme4 pay-list\"><div class=\"list-item\"><div class=\"list-img\"><img src='" + ele.prImage + "'></div>" +
                                "<div class=\"list-mes\"><div class=\"list-info\"><h3 class=\"list-title\">" + ele.name + "</h3></div>" +
                                "<div class=\"list-mes-item\"><span class=\"list-price\">快递单号:" + ele.expressNo + "</span><span class=\"list-price\"><em>¥</em>" + ele.totalPrice + "</span></div>" +
                                "</div></a></article>");
                        });

                        if(deliveries.length > 0){
                            $(".delivery-tab").html("");

                            $(deliveries).each(function(index, ele){
                                $(".delivery-tab").append("<div class=\"pro-box pay-num\"><span>订单编号:" + ele.orderCode + "</span><span class=\"floatr text-red\">" + ele.orderStatus + "</span></div>");
                                $(".delivery-tab").append("<article class=\"m-list list-theme4 pay-list\"><div class=\"list-item\"><div class=\"list-img\"><img src='" + ele.prImage + "'></div>" +
                                    "<div class=\"list-mes\"><div class=\"list-info\"><h3 class=\"list-title\">" + ele.name + "</h3></div>" +
                                    "<div class=\"list-mes-item\"><span class=\"list-price\">快递单号:" + ele.expressNo + "</span><span class=\"list-price\"><em>¥</em>" + ele.totalPrice + "</span></div>" +
                                    "</div></a></article>");
                            });
                        }

                        if(pays.length > 0){
                            $(".pays-tab").html("");

                            $(pays).each(function(index, ele){
                                $(".pays-tab").append("<div class=\"pro-box pay-num\"><span>订单编号:" + ele.orderCode + "</span><span class=\"floatr text-red\">" + ele.orderStatus + "</span></div>");
                                $(".pays-tab").append("<article class=\"m-list list-theme4 pay-list\"><div class=\"list-item\"><div class=\"list-img\"><img src='" + ele.prImage + "'></div>" +
                                    "<div class=\"list-mes\"><div class=\"list-info\"><h3 class=\"list-title\">" + ele.name + "</h3></div>" +
                                    "<div class=\"list-mes-item\"><span class=\"list-price\">快递单号:" + ele.expressNo + "</span><span class=\"list-price\"><em>¥</em>" + ele.totalPrice + "</span></div>" +
                                    "</div></a></article>");
                            });
                        }

                        if(completes.length > 0){
                            $(".complete-tab").html("");

                            $(completes).each(function(index, ele){
                                $(".complete-tab").append("<div class=\"pro-box pay-num\"><span>订单编号:" + ele.orderCode + "</span><span class=\"floatr text-red\">" + ele.orderStatus + "</span></div>");
                                $(".complete-tab").append("<article class=\"m-list list-theme4 pay-list\"><div class=\"list-item\"><div class=\"list-img\"><img src='" + ele.prImage + "'></div>" +
                                    "<div class=\"list-mes\"><div class=\"list-info\"><h3 class=\"list-title\">" + ele.name + "</h3></div>" +
                                    "<div class=\"list-mes-item\"><span class=\"list-price\">快递单号:" + ele.expressNo + "</span><span class=\"list-price\"><em>¥</em>" + ele.totalPrice + "</span></div>" +
                                    "</div></a></article>");
                            });
                        }
                    }
                } else {
                    alert(result.returnMsg);
                    if(result.returnMsg=="客户未登录！"){
                        window.location.href ="/education/wx/customerLogin.html?redirect=/education/wx/integralOrders.html";
                    }
                }
            }
        });
    };

    initOrders();

}(jQuery);